<?php

namespace App\Repositories\Biz;

use Prettus\Repository\Eloquent\BaseRepository as Repository;

class CouponRepository extends Repository
{

    public function model()
    {
        return 'App\Models\Biz\Coupon';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($query = '')
    {
        $staff = \Auth::guard('admin')->user();

        if (!$query) {
            $result = $this->current($staff)->orderBy('id', 'desc')->paginate(10);
        } else {
            $result = $this->current($staff)->where('name', 'like', "%$query%")->paginate(10);
        }

        return $result;
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request)
    {
        \DB::beginTransaction();
        try {
            $shop = $this->create([
                'branch_id' => $request->branch_id,
                'name' => $request->name,
                'type' => $request->type,
                'trade' => $request->trade,
                'category_id' => $request->category_id,
                'member_id' => $request->member_id,
                'staff_id' => $request->staff_id,
                'progress' => $request->progress,
                'address' => $request->address,
                'opening' => $request->opening,
                'date' => time(),
                'status' => 2, //2待审核
            ]);

            \DB::commit();

            return responseSuccess('新增优惠券成功');

        } catch (\Exception $e) {
            \DB::rollback();

            return responseFail(500, '新增优惠券失败');
        }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

}
